<!--  Ofri Keidar 302933981 group 05 -->

<%@page import="model.database.DBException"%>
<%@page import="view.WebCommons"%>
<%@page import="controller.EmployeeController"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<jsp:useBean id="customerData" class="view.useBeans.CustomerData"
	scope="page" />
<jsp:setProperty name="customerData" property="*" />

<html>
<head>
<link rel="stylesheet" href="../../style/myTemplate/style.css"
	type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add new customer</title>
</head>
<body>

	<span class="customHeader2">Add a new customer</span>

	<div id="page">
	<div class="post">

		<div id="sidebar">
			<ul>
				<li>
					<h2>Please insert new customer's personal details</h2>
				</li>
			</ul>
		</div>
		<div id="content">
			<form METHOD=POST ACTION="AddNewCustomer.jsp">
				<table>
					<tr>
						<td><span class="customFormValue">Number</span></td>
						<td><INPUT TYPE=TEXT NAME=number
							SIZE=<%=WebCommons.NUMBER_SIZE%>></td>
					</tr>
					<tr>
						<td><span class="customFormValue">Name</span></td>
						<td><INPUT TYPE=TEXT NAME=name SIZE=<%=WebCommons.NAME_SIZE%>></td>
					</tr>
					<tr>
						<td><span class="customFormValue">Username</span></td>
						<td><INPUT TYPE=TEXT NAME=userName
							SIZE=<%=WebCommons.NAME_SIZE%>></td>
					</tr>
					<tr>
						<td><span class="customFormValue">Address</span></td>
						<td><INPUT TYPE=TEXT NAME=address
							SIZE=<%=WebCommons.ADDRESS_SIZE%>></td>
					</tr>
					<tr>
						<td><span class="customFormValue">Credit Card</span></td>
						<td><INPUT TYPE=TEXT NAME=paymentMethod
							SIZE=<%=WebCommons.NAME_SIZE%>></td>
					</tr>
				</table>

				<br> <INPUT TYPE=SUBMIT class="mySubmitButton">
			</form>
		</div>
	</div>
	</div>
	<%
		// return to main menu
		String menuPage = (String) session.getAttribute(WebCommons.MAIN_MANU);
	%>
	<div id="footer">
	<span class="myLink"><A HREF="<%="../../menus/" + menuPage%>">Back
			to Menu</A></span></div>

	<%
		// check if already submitted details
		if (customerData.isDataSet()) {

			// extract given details
			Integer number = customerData.getCustomerNumberValue();
			String name = customerData.getName();
			String userName = customerData.getUserName();
			String address = customerData.getAddress();
			String creditCard = customerData.getPaymentMethod();

			// verify customer number's format
			if (number == null) {
				%>
				<span class="notify"><br>Please enter a positive integer for
					customer number</span>
				<%
			} else {

				try {

					// extract controller from session
					EmployeeController controller = (EmployeeController) session
							.getAttribute(WebCommons.CONTROLLER);

					// try adding customer
					boolean couldAdd = controller.createCustomerAccount(
							number, name, userName, address, creditCard);

					// check success
					if (!couldAdd) {

						// display error message
						%>
						<span class="notify"><br>Sorry, could not add customer-
							duplicate entry... <br>Please check customer number or username</span>
						<%
					} else {

						// success
						%>
						<span class="notify"><br>Customer added successfully!</span>
						<%
					}

				} catch (DBException e) {

					e.printStackTrace(); // TODO DEBUG PRINT!!!

					// move to error page
					response.sendRedirect("../../commonPages/ErrorPage.jsp");
				}
			}
		}
	%>

</body>
</html>